package com.modular.extra.fm.device.opt;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ToolOpt;
import com.c.opt.ComOpt;
import com.modular.extra.fm.FactoryFmOpt;
import com.modular.extra.fm.device.bean.FmDeviceDo;
import com.tool.ToolRedisson;
import org.redisson.api.RList;

import java.time.Duration;
import java.util.List;

public class OptFmDevice extends ComOpt<FmDeviceDo> {

    public OptFmDevice() {
        super("site_device", FactoryFmOpt.DS_ID);
    }

    /**
     * 查询设备信息 - 根据mn号
     * @param mn
     * @return
     */
    public List<FmDeviceDo> listDevice(String mn) {
        RList<FmDeviceDo> list = ToolRedisson.me.redisson.getList(StrUtil.format("fm-site-device-list-{}", mn));
        if (list.isEmpty()) {
            list.addAll(query().eq("mn", mn).ne("device_type", "5").listBean());
            list.expire(Duration.ofSeconds(60 * 10));
        }
        return list;
    }

    /**
     * 更新设备配置信息
     * @param mn
     */
    public void updateDeviceConfig(String mn) {
        List<FmDeviceDo> fmDeviceDos = listDevice(mn);
        for (FmDeviceDo fmDeviceDo : fmDeviceDos) {
            String comConfig = ToolOpt.OPT_T_D_CONFIG.getComConfig(ObjectUtil.defaultIfBlank(fmDeviceDo.getDeviceNo(), mn));
            fmDeviceDo.setExtendJson(comConfig);
            syncBean(fmDeviceDo);
        }
    }
}
